package cc.autoapi.pucong.auto.flow.provider.node.manager;


import cc.autoapi.pucong.auto.flow.core.root.utils.UUID;
import cc.autoapi.pucong.auto.flow.persistence.po.node.NodeParamsPo;
import cc.autoapi.pucong.auto.flow.persistence.repository.node.NodeParamsService;
import cc.autoapi.pucong.auto.flow.provider.node.entity.NodeParamsConvert;
import cc.autoapi.pucong.auto.flow.provider.node.entity.dto.NodeParamsAddDto;
import cc.autoapi.pucong.auto.flow.provider.node.entity.dto.NodeParamsUpdateDto;
import cc.autoapi.pucong.auto.flow.provider.node.entity.qo.NodeParamsQo;
import cc.autoapi.pucong.auto.flow.provider.node.entity.vo.NodeParamsVo;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author pucong
 * @description 节点参数信息 managerService 实现类
 * @create 2024-09-23
 */
@Service
@Slf4j
public class NodeParamsManager {

    @Resource
    private NodeParamsService nodeParamsService;

    public List<NodeParamsVo> getList(NodeParamsQo qo) {
        List<NodeParamsPo> nodeParamss = nodeParamsService.list(qo.getNodeId(), qo.getParamCode(), qo.getParamType());
        List<NodeParamsVo> nodeParamsVos = NodeParamsConvert.INSTANCE.po2vos(nodeParamss);
        return nodeParamsVos;
    }

    public NodeParamsVo getDetailById(String id) {
        NodeParamsPo nodeParams = nodeParamsService.getById(id);
        NodeParamsVo nodeParamsVo = NodeParamsConvert.INSTANCE.po2vo(nodeParams);
        return nodeParamsVo;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String add(NodeParamsAddDto dto) {
        NodeParamsPo nodeParams = NodeParamsConvert.INSTANCE.dto2po(dto);
        String id = UUID.uuid();
        nodeParams.setUuid(id);
        Boolean i = nodeParamsService.save(nodeParams);
        return id;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateById(String id, NodeParamsUpdateDto dto) {
        NodeParamsPo nodeParamsUpdate = NodeParamsConvert.INSTANCE.dto2po(dto);
        nodeParamsUpdate.setUuid(id);
        Boolean i = nodeParamsService.updateById(nodeParamsUpdate);
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean batchDeleteByIds(List<String> ids) {
        Boolean i = nodeParamsService.removeBatchByIds(ids);
        return i;
    }
}
